package com.example.test06thread.callable;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: fxy
 * @Date: 2022/05/27/10:05
 * @Description:
 */
public class CyclicBarrierTest {
    public static void main(String[] args) {
        CyclicBarrierTest c = new CyclicBarrierTest();
        c.test();
    }
    public void test(){
        CyclicBarrier cyclicBarrier = new CyclicBarrier(10,()-> System.out.println("集齐10张普通卡片，可兑换高级卡片！"));
        int i = 0;
        for (i=0;i<10;i++){
            //lamda不能直接获得i，因为开启一个线程相当于另起一个类；所以需要定义一个final变量，作为中间值
            int finalI = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"这是第"+ finalI +"张卡片");
                try {
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
